#ifndef MPHTTPREQUESTMULTIPARTCONTENTIMPL_H
#define MPHTTPREQUESTMULTIPARTCONTENTIMPL_H


/*****************************************
 * HttpRequestMultipartContentImpl 继承于 HttpRequestMultipartContent
 *****************************************/

#include <vector>
#include <fstream>

#include "mpHttpParams.h"
#include "mpHttpRequestMessage.h"

namespace MP{
namespace HTTP{
namespace INTERNAL{


class HttpRequestMultipartContentImpl : public HttpRequestMultipartContent
{
public:
    HttpRequestMultipartContentImpl(const std::string & boundary);
    ~HttpRequestMultipartContentImpl() override = default;

public:
    virtual void addFormData(const std::string & key, const std::string & val) override;
    virtual void clearFormDatas() override;
    virtual void addFormFile(const std::string & name,
                             const std::string & fileName,
                             const std::string & filePath) override;
    virtual void clearFormFiles() override;
    virtual const std::string& getBoundary() const override;

    virtual void setContentType(const std::string & content_type) override;

    virtual std::string getMd5() override;
    virtual std::size_t getContentLength() override;
    //（如果返回Fasle表示数据存在异常）
    virtual bool beginWriteData() override;
    //如果返回真，则表示仍有数据需要写入；如果返回假，则表示当前数据写入完成后则Payload写入完毕
    virtual bool onWriteDataSome(std::vector<boost::asio::const_buffer> & buffers) override;
    virtual void endWriteData()override;

protected:
    void addBoundary(std::string & str);
    void buildFormData(const std::string & name,
                       const std::string & value,
                       std::string & strFormData);
    void buildFileDataWithOutFileSteam(const std::string & name,
                                       const std::string & fileName,
                                       std::string & strFileData);


    void fillPartContentData(std::vector<boost::asio::const_buffer> & buffers);
    void fillCrlf(std::vector<boost::asio::const_buffer> & buffers);
    void fillEnd(std::vector<boost::asio::const_buffer> & buffers);

private:
    std::string _boundary;
    std::vector<HttpFormData> _parts;
    std::vector<HttpFormFile> _files;

    std::size_t _sendContentLength=0;       //请求体的大小

    bool _isPartWrite = false;              //parts是否已发送
    std::size_t _file_index = 0;            //待发送的文件索引

    std::ifstream _read_file;
    std::streamsize _un_read_size=0;

    std::string _buf_string={};                             //用于存储字符串
    char _buf_file[MP::HTTP::READ_FILE_BUF_SIZE]={};        //用于读取文件数据的数组


};  //HttpRequestMultipartContentImpl


}   //INTERNAL
}   //HTTP
}   //MP


#endif // MPHTTPREQUESTMULTIPARTCONTENTIMPL_H
